cmake_minimum_required(VERSION 3.8)
project(common_test)

# Set to Release in order to speed up the program significantly
# set(CMAKE_BUILD_TYPE Release) #None, Debug, Release, RelWithDebInfo, MinSizeRel
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -O3")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}  -fPIC -g3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -O3")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fPIC -g3")
set(CMAKE_BUILD_TYPE "Release")

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
  # add_compile_options(-Wall -Wextra -Werror -Wno-pedantic -Wno-array-bounds)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(rclcpp_lifecycle REQUIRED)
find_package(common_lib REQUIRED)
find_package(glog REQUIRED)
find_package(nlohmann_json REQUIRED)
find_package(SQLite3 REQUIRED)
find_package(yaml-cpp REQUIRED)
find_package(backward_ros REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(OpenCV REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(cv_bridge REQUIRED)
find_package(image_transport REQUIRED)

# ceres
find_package(Ceres REQUIRED COMPONENTS SuiteSparse)
if(${CERES_VERSION} VERSION_LESS_EQUAL 2.0.0)
  add_definitions(-DUSE_OLD_CERES_API)
endif()

# g2o 
# dpkg -L ros-humble-libg2o
message(STATUS "System: ${CMAKE_SYSTEM}")
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake_modules)
message(STATUS "${CMAKE_MODULE_PATH}")
find_package(SUITESPARSE REQUIRED)
find_package(G2O REQUIRED)
find_package(Boost REQUIRED)

# set(G2O_INCLUDE_DIR "/opt/ros/humble/include")
set(G2O_INCLUDE_DIR "/opt/ros/$ENV{ROS_DISTRO}/include")
set(EXTERNAL_INCLUDE_DIRS ${Eigen_INCLUDE_DIRS} ${SUITESPARSE_INCLUDE_DIRS} ${G2O_INCLUDE_DIR})
set(EXTERNAL_LIBS ${Boost_LIBRARIES} ${SUITESPARSE_LIBRARIES} ${G2O_LIBRARIES})

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()


# export ROS2_WS_ROOT="/home/xxty/trobot/ros2_ws"
# set(WORKSPACE_ROOT "$ENV{ROS2_WS_ROOT}")
# set(COMMON_DIR "${WORKSPACE_ROOT}/src/common")
# MESSAGE(STATUS "COMMON_DIR = ${COMMON_DIR}")

include_directories(
  include
  # ${COMMON_DIR}
  # -ldw
  # ${Eigen_INCLUDE_DIRS}
  ${Boost_INCLUDE_DIRS}
  ${EXTERNAL_INCLUDE_DIRS}
)

set(dependencies
  rclcpp
  rclcpp_action
  rclcpp_lifecycle
  backward_ros
  common_lib
)


# test_sqlite3_node
add_executable(test_sqlite3_node 
  src/test_sqlite3_node.cpp
)
ament_target_dependencies(test_sqlite3_node 
  ${dependencies}
)
target_link_libraries(test_sqlite3_node 
  glog::glog
  SQLite::SQLite3
  nlohmann_json::nlohmann_json
)

# test_file_node
add_executable(test_file_node 
  src/test_file_node.cpp
)
ament_target_dependencies(test_file_node 
  ${dependencies} 
)
target_link_libraries(test_file_node 
  glog::glog
  nlohmann_json::nlohmann_json
  yaml-cpp
)

# test_eigen_node
add_executable(test_eigen_node 
  src/test_eigen_node.cpp
)
ament_target_dependencies(test_eigen_node 
  ${dependencies}
)
target_link_libraries(test_eigen_node 
  glog::glog
  Eigen3::Eigen
)

# test_opencv_node
add_executable(test_opencv_node 
  src/test_opencv_node.cpp
)
ament_target_dependencies(test_opencv_node 
  ${dependencies}
)
target_link_libraries(test_opencv_node 
  glog::glog
  ${OpenCV_LIBS}
)


# test_fitting_curve_eigen
add_executable(test_fitting_curve_eigen 
  src/test_fitting_curve_eigen.cpp
)
ament_target_dependencies(test_fitting_curve_eigen 
  ${dependencies}
)
target_link_libraries(test_fitting_curve_eigen 
  glog::glog
  Eigen3::Eigen
  ${OpenCV_LIBS}
)


# test_fitting_curve_ceres
add_executable(test_fitting_curve_ceres 
  src/test_fitting_curve_ceres.cpp
)
ament_target_dependencies(test_fitting_curve_ceres 
  ${dependencies} 
)
target_link_libraries(test_fitting_curve_ceres 
  glog::glog
  Eigen3::Eigen
  ${OpenCV_LIBS}
  ${CERES_LIBRARIES}
)

# message("CMAKE_SOURCE_DIR = ${CMAKE_SOURCE_DIR}")
# message("CMAKE_BINARY_DIR = ${CMAKE_BINARY_DIR}")
# message("CMAKE_PREFIX_PATH = ${CMAKE_PREFIX_PATH}")
message("G2O头文件路径 = ${G2O_INCLUDE_DIR}")
message("G2O库文件路径 = ${G2O_LIBRARIES}")
# test_fitting_curve_g2o
add_executable(test_fitting_curve_g2o 
  src/test_fitting_curve_g2o.cpp
)
ament_target_dependencies(test_fitting_curve_g2o 
  ${dependencies} 
  # g2o
)
target_link_libraries(test_fitting_curve_g2o 
  glog::glog
  Eigen3::Eigen
  ${OpenCV_LIBS}
  ${EXTERNAL_LIBS}
  # g2o::core
  # g2o::solvers_eigen
  # g2o::types_sba
  # g2o::csparse_extension
  # SuiteSparse::amd
  # SuiteSparse::cholmod
  # SuiteSparse::suitesparseconfig
)


add_executable(test_backward_node 
  src/test_backward_node.cpp
)
ament_target_dependencies(test_backward_node 
  ${dependencies} 
)
target_link_libraries(test_backward_node 
  glog::glog
)


add_executable(test_glog_node 
  src/test_glog_node.cpp
)
ament_target_dependencies(test_glog_node 
  ${dependencies} 
)
target_link_libraries(test_glog_node 
  glog::glog
  -ldw
)

install(TARGETS
  test_sqlite3_node
  test_file_node
  test_eigen_node
  test_opencv_node
  test_fitting_curve_eigen
  test_fitting_curve_ceres
  test_fitting_curve_g2o
  test_backward_node
  test_glog_node
  DESTINATION lib/${PROJECT_NAME}
)

ament_package()
